package com.example.springboot.controller;

import com.example.springboot.common.CommentPage;
import com.example.springboot.common.Constants;
import com.example.springboot.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;
import com.example.springboot.common.Result;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.example.springboot.service.ICCommentService;
import com.example.springboot.entity.CComment;

import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/c-comment")
    public class CCommentController {
    
    @Resource
    private ICCommentService cCommentService;

    // 新增评论
    @PostMapping
    public Result save(@RequestBody CComment cComment) throws IOException {
        cComment.setUserId(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        CComment comment = cCommentService.insert(cComment);
        if (comment == null) {  //父评论被删除
            return Result.error(Constants.CODE_600,"评论不存在");
        }
        else
            return Result.success(true);
    }

    // 更新评论
    @PostMapping("/update")
    public Result update(@RequestBody CComment cComment) {
        return Result.success(cCommentService.saveOrUpdate(cComment));
    }

    //根据id删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean result = cCommentService.deleteOne(id);
        if (result)
            return Result.success(true);
        else
            return Result.error(Constants.CODE_600,"该评论已被删除");
    }

    //根据id批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(cCommentService.removeByIds(ids));
    }

    //根据创意查询评论相关信息
    @GetMapping("/getCommentsByCreative")
    public Result getCommentsByCreative(@RequestParam Integer creativeId) {
        return Result.success(cCommentService.getCommentsByCreative(creativeId));
    }

    //根据id查找
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(cCommentService.getById(id));
    }

    //根据方案id返回分页评论
    @GetMapping("/getByCreative")
    public Result getCommentsByCreative(@RequestParam(defaultValue = "1") int PageNum,
                                        @RequestParam(defaultValue = "10") int PageSize,
                                        @RequestParam Integer creative_id) {
        CommentPage page = new CommentPage();
        return Result.success(page.ccommentPage(cCommentService.getCommentsByCreative(creative_id),PageNum,PageSize));
    }

    //管理员获取评论列表
    @GetMapping("/adminGet")
    public Result adminGet(@RequestParam(defaultValue = "1") int PageNum,
                           @RequestParam(defaultValue = "10") int PageSize,
                           @RequestParam(defaultValue = "") String content) {
        return cCommentService.adminGet(PageNum,PageSize,content);
    }

    //管理员处理违规评论
    @PostMapping("/processComment")
    private Result processComment(@RequestBody Map<String, Integer> commentMap) {
        return cCommentService.processComment(commentMap.get("id"));
    }
}

